# 集合 set
# 特点：无重复，无序(无下标)
# 符号: {} {元素,元素,元素,...} --> set
#      {} {key:value,key:value,...} --> dict
# 定义集合：
set1 = {'张三'}
print(type(set1)) # <class 'set'>

list1 = [1, 2, 3, 4, 5, 6, 2, 3, 9, 10]

set2 =set(list1)
print(set2) # {1, 2, 3, 4, 5, 6, 9, 10}

set3 = set(list1)
print(set3) # {1, 2, 3, 4, 5, 6, 9, 10}

set3 = set() # 空集合
print(type(set3)) # <class 'set'>

print(len(set3)) # 0

# 添加元素
set3.add('三体')
print(set3) # {'三体'}

set3.add('流浪地球')
print(set3) # {'三体', '流浪地球'}

# append extend insert --> list
# add update --> set
# list: 有序，允许重复
# set: 无序，不允许重复

set1.update(set3)
print(set1) # {'三体', '流浪地球'}

print(set3)
print('------------------------------------')
'''
产生5组（不允许重复），字母和数字组成的4位验证码，并打印
'''
import random
code_list = set()
s = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
while True:
    code = ''
    for i in range(4):
        r = random.choice(s)
        code += r
    code_list.add(code)
    if len(code_list) == 5:
        break
print(code_list)
print('------------------------------------')
set4 = {'zjW4', 'BkIt', '67M9', 'vqbh', 'D6n2'}

# set4.remove('D6n2') # 如果不存在，会报错
# print(set4)

# set4.discard('D6n2') # 如果不存在，不会报错
# print(set4)

# set4.clear()
# set4.add('zjW4')
# print(set4) # set()

set4.pop() # 随机删除一个元素
print(set4) # {'zjW4', 'BkIt', '67M9', 'vqbh'}

# 集合： 交集 intersection 并集 union 差集 difference
set5 = {1, 2, 3, 4, 5}
set6 = {3, 4, 5, 6, 7, 8, 9}
result = set5.intersection(set6) # = print(set5 & set6)
print(result) # {3, 4, 5}

result = set5.union(set6)  # = print(set5|set6)
print(result) # {1, 2, 3, 4, 5, 6, 7, 8, 9}

result = set5.difference(set6) # = print(set5 - set6)
print(result) # {1, 2}

'''
类型转换
tuple --> list,set
set --> list,tuple
dict --> list,tuple,set 但只将字典的键放到了[],(),{}
list --> dict
特殊要求：list1 = [('a',10), ('b',20), ('c',30)]
print(dict(list1)) # {'a': 10, 'b': 20, 'c': 30}
'''
